package com.woniuxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.woniuxy.dto.commonDto.BaseDto;
import com.woniuxy.dto.GoodsPropDto;
import com.woniuxy.entity.GoodsProp;
import com.woniuxy.entity.GoodsPropgroup;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.GoodsFiledException;
import com.woniuxy.exception.PawnUserException;
import com.woniuxy.mapper.GoodsPropMapper;
import com.woniuxy.param.*;
import com.woniuxy.service.GoodsPropService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.GoodsPropgroupService;
import com.woniuxy.util.PageMysqlUtil;
import com.woniuxy.util.WoniuBeanUtils;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 属性 服务实现类
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2022-01-04
 */
@Service
public class GoodsPropServiceImpl extends ServiceImpl<GoodsPropMapper, GoodsProp> implements GoodsPropService {

    @Resource
    private GoodsPropgroupService goodsPropgroupService;


    @Override
    public BaseDto<GoodsPropDto> listGoodsPropBySmallerId(PageQueryParam<KeyAndIdParam> pqp) {

        GoodsPropgroup gpg = goodsPropgroupService.getById(pqp.getQueryCriteria().getId());
        if (StringUtils.isEmpty(gpg))
            throw new GoodsFiledException(CodeEnum.GOODS_PROP_NULL.getMessage(), CodeEnum.GOODS_PROP_NULL.getCode());

        QueryWrapper<GoodsProp> query = new QueryWrapper<>();
        //属性组id
        query.eq("goods_propgroup_id", gpg.getGoodsPropgroupId())
                //能查看的类型
                .eq("goods_prop_del", 0);
        if (!StringUtils.isEmpty(pqp.getQueryCriteria().getKey())) {
            query.and(wrapper -> wrapper
                    .like("goods_prop_name", pqp.getQueryCriteria().getKey())
                    .or()
                    .like("goods_prop_select", pqp.getQueryCriteria().getKey())
            );
        }

        BaseDto<GoodsPropDto> roleDtoBaseDto = PageMysqlUtil.pageQuery(GoodsPropDto.class, this, query, new BaseParam(pqp.getIndexPage(), pqp.getPageSize()));

        return roleDtoBaseDto;
    }

    @Override
    public GoodsPropDto getGoodsPropById(Integer goodsPropId) {

        GoodsProp one = getOne(new QueryWrapper<GoodsProp>().eq("goods_prop_del", 0).eq("goods_prop_id", goodsPropId));

        String select = one.getGoodsPropSelect();
        Gson gson = new Gson();


        String[] strings = gson.fromJson(select, String[].class);


        GoodsPropDto dto = WoniuBeanUtils.copyObject(one, GoodsPropDto.class);

        dto.setSelect(strings);
        return dto;
    }

    @Override
    public boolean saveGoodsProp(SaveGoodsPropParam sgpp) {
        //获取属性组
        GoodsPropgroup gpg = goodsPropgroupService.getById(sgpp.getGoodsPropgroupId());
        if (StringUtils.isEmpty(gpg))
            throw new GoodsFiledException(CodeEnum.GOODS_PROP_NULL.getMessage(), CodeEnum.GOODS_PROP_NULL.getCode());
        //一个数组复选单选唯一选只能各一个
        List<GoodsProp> goodsProps = list(new QueryWrapper<GoodsProp>().eq("goods_propgroup_id", gpg.getGoodsPropgroupId()).eq("goods_prop_del", 0));
        if (goodsProps.size() >= 3)
            throw new GoodsFiledException(CodeEnum.GOODS_SAVE_FAIL.getMessage(), CodeEnum.GOODS_SAVE_FAIL.getCode());
        else {
            for (GoodsProp goodsProp : goodsProps) {
                if (goodsProp.getGoodsPropClass() == sgpp.getGoodsPropClass())
                    throw new GoodsFiledException(CodeEnum.GOODS_SAVE_FAIL.getMessage(), CodeEnum.GOODS_SAVE_FAIL.getCode());
            }
        }
//        Gson gson = new Gson();
        String[] select = sgpp.getSelect();
        JSONArray jsonArray = null;
        try {
            jsonArray = new JSONArray(select);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        GoodsProp prop = WoniuBeanUtils.copyObject(sgpp, GoodsProp.class);

        prop.setGoodsPropSelect(jsonArray.toString());

        return save(prop);
    }

    @Override
    public boolean updateGoodsProp(UpdateGoodsPropParam updateGoodsPropParam) {

        GoodsProp goodsProp = getOne(new QueryWrapper<GoodsProp>().eq("goods_prop_id", updateGoodsPropParam.getGoodsPropId()).eq("goods_prop_del", 0));

        if (StringUtils.isEmpty(goodsProp))
            throw new GoodsFiledException(CodeEnum.GOODS_PROP_NULL.getMessage(), CodeEnum.GOODS_PROP_NULL.getCode());


        GoodsPropgroup gpg = goodsPropgroupService.getById(updateGoodsPropParam.getGoodsPropgroupId());
        if (StringUtils.isEmpty(gpg))
            throw new GoodsFiledException(CodeEnum.GOODS_PROP_NULL.getMessage(), CodeEnum.GOODS_PROP_NULL.getCode());

        if (goodsProp.getGoodsPropClass() != updateGoodsPropParam.getGoodsPropClass()) {



            List<GoodsProp> list = list(new QueryWrapper<GoodsProp>()
                    .eq("goods_prop_del", 0).eq("goods_prop_class", updateGoodsPropParam.getGoodsPropClass()));


            if (null != list && list.size() > 0) {
                throw new GoodsFiledException(CodeEnum.GOODS_UPDATE_FAIL.getMessage(), CodeEnum.GOODS_UPDATE_FAIL.getCode());
            }
        }

        Gson gson = new Gson();
        String[] select = updateGoodsPropParam.getSelect();
        JSONArray jsonArray = null;
        try {
            jsonArray = new JSONArray(select);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        GoodsProp prop = WoniuBeanUtils.copyObject(updateGoodsPropParam, GoodsProp.class);

        prop.setGoodsPropSelect(jsonArray.toString());
        return updateById(prop);

    }

    @Override
    public boolean removeGoodsPropByIds(List<Integer> ids) {

        List<GoodsProp> list = listByIds(ids);
        if (ids.size() != list.size())
            throw new PawnUserException(CodeEnum.USER_DELETE_FAIL_1.getMessage(), CodeEnum.USER_DELETE_FAIL_1.getCode());
        for (GoodsProp goodsProp : list) {
            goodsProp.setGoodsPropDel(1);
        }
        return updateBatchById(list);

    }


}
